Mail 您所在的位置:网站首页 Laravel 10 Attach File to Email From Storage Example Mail

Mail

2024-05-18 14:01| 来源: 网络整理| 查看: 265

Mail Introduction Configuration Driver Prerequisites Failover Configuration Round Robin Configuration Generating Mailables Writing Mailables Configuring the Sender Configuring the View View Data Attachments Inline Attachments Attachable Objects Headers Tags and Metadata Customizing the Symfony Message Markdown Mailables Generating Markdown Mailables Writing Markdown Messages Customizing the Components Sending Mail Queueing Mail Rendering Mailables Previewing Mailables in the Browser Localizing Mailables Testing Testing Mailable Content Testing Mailable Sending Mail and Local Development Events Custom Transports Additional Symfony Transports

Introduction

Sending email doesn't have to be complicated. Laravel provides a clean, simple email API powered by the popular Symfony Mailer component. Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud based service of your choice.

Configuration

Laravel's email services may be configured via your application's config/mail.php configuration file. Each mailer configured within this file may have its own unique configuration and even its own unique "transport", allowing your application to use different email services to send certain email messages. For example, your application might use Postmark to send transactional emails while using Amazon SES to send bulk emails.

Within your mail configuration file, you will find a mailers configuration array. This array contains a sample configuration entry for each of the major mail drivers / transports supported by Laravel, while the default configuration value determines which mailer will be used by default when your application needs to send an email message.

Driver / Transport Prerequisites

The API based drivers such as Mailgun, Postmark, and MailerSend are often simpler and faster than sending mail via SMTP servers. Whenever possible, we recommend that you use one of these drivers.

Mailgun Driver

To use the Mailgun driver, install Symfony's Mailgun Mailer transport via Composer:

composer require symfony/mailgun-mailer symfony/http-client

Next, set the default option in your application's config/mail.php configuration file to mailgun. After configuring your application's default mailer, verify that your config/services.php configuration file contains the following options:

'mailgun' => [ 'transport' => 'mailgun', 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'),],

If you are not using the United States Mailgun region, you may define your region's endpoint in the services configuration file:

'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), 'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),],

Postmark Driver

To use the Postmark driver, install Symfony's Postmark Mailer transport via Composer:

composer require symfony/postmark-mailer symfony/http-client

Next, set the default option in your application's config/mail.php configuration file to postmark. After configuring your application's default mailer, verify that your config/services.php configuration file contains the following options:

'postmark' => [ 'token' => env('POSTMARK_TOKEN'),],

If you would like to specify the Postmark message stream that should be used by a given mailer, you may add the message_stream_id configuration option to the mailer's configuration array. This configuration array can be found in your application's config/mail.php configuration file:

'postmark' => [ 'transport' => 'postmark', 'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'),],

This way you are also able to set up multiple Postmark mailers with different message streams.

SES Driver

To use the Amazon SES driver you must first install the Amazon AWS SDK for PHP. You may install this library via the Composer package manager:

composer require aws/aws-sdk-php

Next, set the default option in your config/mail.php configuration file to ses and verify that your config/services.php configuration file contains the following options:

'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),],

To utilize AWS temporary credentials via a session token, you may add a token key to your application's SES configuration:

'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'token' => env('AWS_SESSION_TOKEN'),],

If you would like to define additional options that Laravel should pass to the AWS SDK's SendEmail method when sending an email, you may define an options array within your ses configuration:

'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'options' => [ 'ConfigurationSetName' => 'MyConfigurationSet', 'EmailTags' => [ ['Name' => 'foo', 'Value' => 'bar'], ], ],],

MailerSend Driver

MailerSend, a transactional email and SMS service, maintains their own API based mail driver for Laravel. The package containing the driver may be installed via the Composer package manager:

composer require mailersend/laravel-driver

Once the package is installed, add the MAILERSEND_API_KEY environment variable to your application's .env file. In addition, the MAIL_MAILER environment variable should be defined as mailersend:

MAIL_MAILER=mailersendMAIL_FROM_ADDRESS=[email protected]MAIL_FROM_NAME="App Name" MAILERSEND_API_KEY=your-api-key

To learn more about MailerSend, including how to use hosted templates, consult the MailerSend driver documentation.

Failover Configuration

Sometimes, an external service you have configured to send your application's mail may be down. In these cases, it can be useful to define one or more backup mail delivery configurations that will be used in case your primary delivery driver is down.

To accomplish this, you should define a mailer within your application's mail configuration file that uses the failover transport. The configuration array for your application's failover mailer should contain an array of mailers that reference the order in which configured mailers should be chosen for delivery:

'mailers' => [ 'failover' => [ 'transport' => 'failover', 'mailers' => [ 'postmark', 'mailgun', 'sendmail', ], ],  // ...],

Once your failover mailer has been defined, you should set this mailer as the default mailer used by your application by specifying its name as the value of the default configuration key within your application's mail configuration file:

'default' => env('MAIL_MAILER', 'failover'),

Round Robin Configuration

The roundrobin transport allows you to distribute your mailing workload across multiple mailers. To get started, define a mailer within your application's mail configuration file that uses the roundrobin transport. The configuration array for your application's roundrobin mailer should contain an array of mailers that reference which configured mailers should be used for delivery:

'mailers' => [ 'roundrobin' => [ 'transport' => 'roundrobin', 'mailers' => [ 'ses', 'postmark', ], ],  // ...],

Once your round robin mailer has been defined, you should set this mailer as the default mailer used by your application by specifying its name as the value of the default configuration key within your application's mail configuration file:

'default' => env('MAIL_MAILER', 'roundrobin'),

The round robin transport selects a random mailer from the list of configured mailers and then switches to the next available mailer for each subsequent email. In contrast to failover transport, which helps to achieve high availability, the roundrobin transport provides load balancing.

Generating Mailables

When building Laravel applications, each type of email sent by your application is represented as a "mailable" class. These classes are stored in the app/Mail directory. Don't worry if you don't see this directory in your application, since it will be generated for you when you create your first mailable class using the make:mail Artisan command:

php artisan make:mail OrderShipped

Writing Mailables

Once you have generated a mailable class, open it up so we can explore its contents. Mailable class configuration is done in several methods, including the envelope, content, and attachments methods.

The envelope method returns an Illuminate\Mail\Mailables\Envelope object that defines the subject and, sometimes, the recipients of the message. The content method returns an Illuminate\Mail\Mailables\Content object that defines the Blade template that will be used to generate the message content.

Configuring the Sender

Using the Envelope

First, let's explore configuring the sender of the email. Or, in other words, who the email is going to be "from". There are two ways to configure the sender. First, you may specify the "from" address on your message's envelope:

use Illuminate\Mail\Mailables\Address;use Illuminate\Mail\Mailables\Envelope; /** * Get the message envelope. */public function envelope(): Envelope{ return new Envelope( from: new Address('[email protected]', 'Jeffrey Way'), subject: 'Order Shipped', );}

If you would like, you may also specify a replyTo address:

return new Envelope( from: new Address('[email protected]', 'Jeffrey Way'), replyTo: [ new Address('[email protected]', 'Taylor Otwell'), ], subject: 'Order Shipped',);

Using a Global from Address

However, if your application uses the same "from" address for all of its emails, it can become cumbersome to add it to each mailable class you generate. Instead, you may specify a global "from" address in your config/mail.php configuration file. This address will be used if no other "from" address is specified within the mailable class:

'from' => [ 'address' => env('MAIL_FROM_ADDRESS', '[email protected]'), 'name' => env('MAIL_FROM_NAME', 'Example'),],

In addition, you may define a global "reply_to" address within your config/mail.php configuration file:

'reply_to' => ['address' => '[email protected]', 'name' => 'App Name'],

Configuring the View

Within a mailable class's content method, you may define the view, or which template should be used when rendering the email's contents. Since each email typically uses a Blade template to render its contents, you have the full power and convenience of the Blade templating engine when building your email's HTML:

/** * Get the message content definition. */public function content(): Content{ return new Content( view: 'mail.orders.shipped', );}

[!NOTE] You may wish to create a resources/views/emails directory to house all of your email templates; however, you are free to place them wherever you wish within your resources/views directory.

Plain Text Emails

If you would like to define a plain-text version of your email, you may specify the plain-text template when creating the message's Content definition. Like the view parameter, the text parameter should be a template name which will be used to render the contents of the email. You are free to define both an HTML and plain-text version of your message:

/** * Get the message content definition. */public function content(): Content{ return new Content( view: 'mail.orders.shipped', text: 'mail.orders.shipped-text' );}

For clarity, the html parameter may be used as an alias of the view parameter:

return new Content( html: 'mail.orders.shipped', text: 'mail.orders.shipped-text');

View Data

Via Public Properties

Typically, you will want to pass some data to your view that you can utilize when rendering the email's HTML. There are two ways you may make data available to your view. First, any public property defined on your mailable class will automatically be made available to the view. So, for example, you may pass data into your mailable class's constructor and set that data to public properties defined on the class:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有